package com.aiit.controller;

import com.aiit.pojo.LingJian;
import com.aiit.pojo.NameValue;
import com.aiit.pojo.Merchant;
import com.aiit.pojo.order.StringObject;
import com.aiit.service.ILingJianService;
import com.aiit.service.IMerchantService;
import com.aiit.service.IPurchaseOrderService;
import com.aiit.service.ISaleOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/report")
public class ReportController {
    @Autowired
    IMerchantService merchantService;
    @Autowired
    IPurchaseOrderService purchaseOrderService;
    @Autowired
    ISaleOrderService saleOrderService;
    @Autowired
    ILingJianService lingJianService;

    @RequestMapping("/index")
    public String toIndex(){
            return "/main/report/index";
        }
    @RequestMapping("/report")
    public String toReport(){
        return "/main/report/report";
    }
    //完成进货数据查询
    @ResponseBody
    @RequestMapping("/login31/index")
    public Map<String, Object> login31Index(){
        Map<String, Object> map = new HashMap<>();
        List<Merchant> merchants = merchantService.queryAll();
        List<NameValue> lnas = new ArrayList<>();
        for(int i=0;i<merchants.size();i++){
            NameValue nameValue = new NameValue();
            nameValue.setName(merchants.get(i).getName());
            nameValue.setValue(merchants.get(i).getSuccessNumber());
            lnas.add(nameValue);
        }
        System.out.println(lnas);
        map.put("lnas",lnas);
        map.put("merchants",merchants);
        return map;
    }
    //完成退货数据查询
    @ResponseBody
    @RequestMapping("/login32/index")
    public Map<String, Object> login32Index(){
        Map<String, Object> map = new HashMap<>();
        List<Merchant> merchants = merchantService.queryAll();
        List<NameValue> lnas = new ArrayList<>();
        for(int i=0;i<merchants.size();i++){
            NameValue nameValue = new NameValue();
            nameValue.setName(merchants.get(i).getName());
            nameValue.setValue(merchants.get(i).getTimeoutNumber());
            lnas.add(nameValue);
        }
        System.out.println(lnas);
        map.put("lnas",lnas);
        map.put("merchants",merchants);
        return map;
    }
    //交易总数据查询
    @ResponseBody
    @RequestMapping("/login33/index")
    public Map<String, Object> login33Index(){
        Map<String, Object> map = new HashMap<>();
        List<Merchant> merchants = merchantService.queryAll();
        List<NameValue> lnas = new ArrayList<>();
        for(int i=0;i<merchants.size();i++){
            NameValue nameValue = new NameValue();
            nameValue.setName(merchants.get(i).getName());
            nameValue.setValue(merchants.get(i).getTotalNumber());
            lnas.add(nameValue);
        }
        System.out.println(lnas);
        map.put("lnas",lnas);
        map.put("merchants",merchants);
        return map;
    }
    //商家及其每种商品交易数据查询
    @ResponseBody
    @RequestMapping("/login/echart1")
    public Map<String, Object> loginEchart1(){
        Map<String, Object> map = new HashMap<>();
        List<Merchant> merchants = merchantService.queryAll();
        List<String> merchantName = new ArrayList<>();
        List<String> lingJianName = new ArrayList<>();
        List<LingJian> lingJians = lingJianService.queryAll();
        merchants.forEach(merchant ->merchantName.add(merchant.getName()));
        List<Integer> list = new ArrayList<>();
        List<StringObject> list2 = new ArrayList<>();
        for (int i = 0;i<lingJians.size();i++){
            List<Integer> ilists = new ArrayList<>();
            StringObject sObject = new StringObject();
            for (int j=0;j<merchants.size();j++){
                if (purchaseOrderService.queryPOSByMLId(merchants.get(j).getId(),lingJians.get(i).getId())==0){
                    ilists.add(0);
                }else {
                    ilists.add(purchaseOrderService.queryPOSByMLId2(merchants.get(j).getId(), lingJians.get(i).getId()));
                }
                sObject.setName(lingJians.get(i).getName());
                sObject.setValue(ilists);
            }
            list2.add(sObject);
        }
        map.put("lists",list2);
        map.put("merchantName",merchantName);
        return map;
    }
    //每种商品进货数据查询
    @ResponseBody
    @RequestMapping("/login/echart2")
    public Map<String, Object> loginEchart2(){
        Map<String, Object> maps = new HashMap<>();
        List<NameValue> lists = new ArrayList<>();
        lists = purchaseOrderService.queryAllSun();
        System.out.println(lists);
        maps.put("lists",lists);
        return maps;
    }
    //每种商品交易数据查询
    @ResponseBody
    @RequestMapping("/login/echart5")
    public Map<String, Object> loginEchart5(){
        Map<String, Object> maps = new HashMap<>();
        List<NameValue> lists = new ArrayList<>();
        lists = saleOrderService.queryAllSum();
        System.out.println(lists);
        maps.put("lists",lists);
        return maps;
    }
    //当前商品库存数据查询
    @ResponseBody
    @RequestMapping("/login/echart6")
    public Map<String, Object> loginEchart6(){
        Map<String, Object> maps = new HashMap<>();
        List<NameValue> lists = new ArrayList<>();
        lists = lingJianService.queryAllNN();
        maps.put("lists",lists);
        return maps;
    }
}
